const addressList = [
	{
		id: 1,
		addressDetail: "杭州市西湖区 黄龙万科中心",
		areaCode: "110101",
		city: "北京市",
		province: "北京市",
		county: "东城区",
		country: "",
		isDefault: false,
		name: "11",
		postalCode: "000000",
		tel: "13691163111",
	},
	{
		id: 2,
		addressDetail: "杭州市西湖区 黄龙万科中心",
		areaCode: "110101",
		city: "北京市",
		province: "北京市",
		county: "东城区",
		country: "",
		isDefault: false,
		name: "bbb",
		postalCode: "000000",
		tel: "13691163715",
	},
];
const bodyParser = require("body-parser");
module.exports = {
	devServer: {
		before(app) {
			app.use(bodyParser.json());
			// 获取地址列表
			app.get("/api/address", (req, res) => {
				res.send({
					success: true,
					data: addressList,
				});
			});
			// 新增地址
			// query body params
			app.post("/api/address", (req, res) => {
				addressList.push({
					...req.body,
					id: Date.now(),
				});
				res.send({
					success: true,
					message: "添加成功",
				});
			});
			// 编辑地址
			app.post("/api/address/edit", (req, res) => {
				const { id } = req.body;
				// 数组方法
				// forEach遍历 filter过滤返回过滤后数组 map返回return的所有结果 find找到目标元素返回 findIndex找到下标返回
				// some一真则真 返回bool every 一假则假 返回bool
				const ind = addressList.findIndex(item => {
					return item.id == id;
				});
				addressList[ind] = { ...req.body };
				res.send({
					success: true,
					message: "修改成功",
				});
			});
			// 删除地址
			app.get("/api/address/delete/:id", (req, res) => {
				const { id } = req.params;
				// 数组方法
				// forEach遍历 filter过滤返回过滤后数组 map返回return的所有结果 find找到目标元素返回 findIndex找到下标返回
				// some一真则真 返回bool every 一假则假 返回bool
				const ind = findIndex(item => {
					item.id == id;
				});
				// 删除数组中的某一位
				// slice splice
				addressList.splice(ind, 1);
				res.send({
					success: true,
					message: "添加成功",
				});
			});
		},
		proxy: {
			// 另起一个服务 发起请求  服务器不存在同源策略 所以可以跨
			"/api": {
				target: "http://localhost:8080",
				pathRewrite: {
					"^/api": "/api",
				},
			},
		},
	},
};
